<template>
  <div class="base-container">
    <t-row :gutter="13">
      <t-col :span="2">
        <div style="display: flex;font-weight: bold;font-size: 30px;padding: 30px">
          学科分类
        </div>
        <t-tree checkable activable @change="onChange" v-model="queryParams.subjectClassifications"
          :data="dict.type.subject_classification" :expandAll="false" hover :expand-on-click-node="false" />
      </t-col>
      <t-col :span="10">
        <t-form ref="form" :data="queryParams" label-width="80"
          @submit="() => { this.pagination.current = 1; this.queryParams.pageNum = 1; this.getList() }">
          <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
            <t-col :span="11" class="query-form-inline">
              <t-form-item label="项目名称" name="projectName">
                <t-input v-model="queryParams.projectName" clearable placeholder="请输入项目名称" style="width: 240px" />
              </t-form-item>
            </t-col>
            <t-col :span="1" class="operation-container">
              <t-button theme="primary" type="submit"> 查询</t-button>
              <t-button theme="default" variant="outline" type="reset">重置</t-button>
            </t-col>
          </t-row>
        </t-form>
        <div style="display: flex;font-weight: bold;justify-content: center;font-size: 30px;padding: 30px">
          待形式审查项目列表
        </div>
        <t-tabs v-if="site_tag === 'jtt'" @change="(val) => { queryParams.projectCategory = val; getList(); }"
          v-model="queryParams.projectCategory" style="margin-bottom: 10px">
          <t-tab-panel :key="item.id" :value="item.id"
            v-for="(item) in [{ 'name': '全部', id: null }, ...projectCategoryOptions]" :label="item.name"
            :destroyOnHide="false">
          </t-tab-panel>
        </t-tabs>
        <t-table maxHeight="400" row-key="id"
          :columns="site_tag === 'jtt' ? columns : columns.filter(val => { return val.title !== '计划类别' })" :data="data"
          :selected-row-keys="selectedRowKeys" :loading="dataLoading"
          @select-change="(val) => { selectedRowKeys = val }">
          <template #[`projectNum`]="{ row }">
            <div v-if="row.projectNum">
              {{ row.projectNum }}
            </div>
            <div v-else>
              暂未分配
            </div>
          </template>
          <template #projectName="{ row }">
            <t-link v-if="row.version === '2024'"
              @click="dialog.objId = row.id; dialog.header = row.projectName; dialog.open_item_detail = true"
              hover="color" theme="primary">{{ row.projectName }}</t-link>

            <t-link v-else @click="dialog.objId = row.id; dialog.header = row.projectName; dialog.open_detail = true"
              hover="color" theme="primary">{{ row.projectName }}</t-link>
          </template>
          <template #[`adviceFunding`]="{ row }">
            {{ row.adviceFunding }}万元
          </template>
          <template #[`subsidyFunding`]="{ row }">
            {{ row.subsidyFunding }}万元
          </template>
          <template #[`subjectClassification`]="{ row }">
            {{ row.subjectClassification | filterByDict(dict.type.subject_classification) }}
          </template>
          <template #projectType="{ row }">
            {{ row.projectType | filterByDict(dict.type.project_type) }}
          </template>
          <template #op="{ row }">
            <t-button v-if="row.version === '2024'"
              @click="dialog.header = row.projectName; dialog.objId = row.id; dialog.editOpen = false; dialog.open_item_detail = true"
              size="small" variant="text" class="t-button-link">
              <t-icon name="info-circle" slot="icon" />
              详情
            </t-button>
            <t-button v-else
              @click="dialog.header = row.projectName; dialog.objId = row.id; dialog.editOpen = false; dialog.open_detail = true"
              size="small" variant="text" class="t-button-link">
              <t-icon name="info-circle" slot="icon" />
              详情
            </t-button>
          </template>
        </t-table>
        <div style="display: flex;font-weight: bold;justify-content: center;gap:10px;margin-top:30px">
          <t-button :disabled="this.selectedRowKeys.length <= 0" @click="joinList" size="large" theme="default"
            variant="outline">
            <t-icon name="arrow-down" slot="icon"></t-icon>分配</t-button>
          <t-button :disabled="this.selectedRowKeys2.length <= 0" @click="removeList" size="large" theme="default"
            variant="outline"> <t-icon name="arrow-up" slot="icon"></t-icon>移出</t-button>
          <t-button size="large" @click="onSubmit" theme="primary" :loading="applyLoading">{{
            applyLoading ? '提交中...' : '点击提交'
          }}</t-button>
        </div>
        <div style="display: flex;font-weight: bold;justify-content: center;font-size: 30px;padding: 30px">
          已分配项目列表
        </div>
        <t-table maxHeight="400" row-key="id"
          :columns="site_tag === 'jtt' ? columns2 : columns2.filter(val => { return val.title !== '计划类别' })"
          :data="data2" :selected-row-keys="selectedRowKeys2" :loading="dataLoading2"
          @select-change="(val) => { selectedRowKeys2 = val }">
          <template #[`projectName`]="{ row }">
            <t-link v-if="row.version === '2024'"
              @click="dialog.objId = row.id; dialog.header = row.projectName; dialog.open_item_detail = true"
              hover="color" theme="primary">{{ row.projectName }}</t-link>

            <t-link v-else @click="dialog.objId = row.id; dialog.header = row.projectName; dialog.open_detail = true"
              hover="color" theme="primary">{{ row.projectName }}</t-link>
          </template>
          <template #[`adviceFunding`]="{ row }">
            {{ row.adviceFunding }}万元
          </template>
          <template #[`subsidyFunding`]="{ row }">
            {{ row.subsidyFunding }}万元
          </template>
          <template #[`subjectClassification`]="{ row }">
            {{ row.subjectClassification | filterByDict(dict.type.subject_classification) }}
          </template>
          <template #[`projectType`]="{ row }">
            {{ row.projectType | filterByDict(dict.type.project_type) }}
          </template>
          <template #[`xsReviewStatus`]="{ row }">
            <t-tag v-if="row.xsReviewStatus"
              @click="dialog.header = row.projectName; dialog.objId = row.id; dialog.open_approve_list = true"
              class="cursor-pointer" :theme="handleTheme(row.xsReviewStatus, dict.type.xs_review_status)"
              variant="light">
              {{ row.xsReviewStatus | filterByDict(dict.type.xs_review_status) }}
            </t-tag>
            <t-tag @click="dialog.header = row.projectName; dialog.objId = row.id; dialog.open_approve_list = true"
              class="cursor-pointer" v-else theme="warning" variant="light">未评审</t-tag>
          </template>
          <template #op="{ row }">
            <t-button v-if="row.version === '2024'"
              @click="dialog.header = row.projectName; dialog.objId = row.id; dialog.editOpen = false; dialog.open_item_detail = true"
              size="small" variant="text" class="t-button-link">
              <t-icon name="info-circle" slot="icon" />
              详情
            </t-button>
            <t-button v-else
              @click="dialog.header = row.projectName; dialog.objId = row.id; dialog.editOpen = false; dialog.open_detail = true"
              size="small" variant="text" class="t-button-link">
              <t-icon name="info-circle" slot="icon" />
              详情
            </t-button>
          </template>
        </t-table>
      </t-col>
    </t-row>

    <project-detail-dialog :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_detail"
      v-if="dialog.open_detail"></project-detail-dialog>
    <item-detail-dialog :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_item_detail"
      v-if="dialog.open_item_detail"></item-detail-dialog>
    <approve-list :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_approve_list"
      v-if="dialog.open_approve_list"></approve-list>

  </div>
</template>

<script>
import ApproveList from "./components/approve-list.vue";
import ProjectDetailDialog from "../pm/components/project-detail-dialog.vue";
import ItemDetailDialog from "../pm/components/item-detail-dialog.vue";

export default {
  name: "select-project",
  dicts: ["subject_classification", "project_type", "xs_review_status"],
  components: {
    ApproveList,
    ProjectDetailDialog,
    ItemDetailDialog
  },
  data () {
    return {
      userId: null,
      site_tag: import.meta.env.VITE_SITE_TAG,
      applyLoading: false,
      data2: [],
      dataLoading2: false,
      selectedRowKeys2: [],
      projectCategoryOptions: [],
      dialog: {
        header: "",
        objId: 0,
        open_detail: false,
        editOpen: true,
        open_approve_list: false,
        open_item_detail: false,
      },
      selectedRowKeys: [],
      queryParams: {
        projectName: '',
        reviewType: '',
        pageNum: 1,
        pageSize: 10,
        dateRange: [],
        subjectClassifications: [],
        isAddReview: '0',
        projectCategory: null,
      },
      data: [],
      dataLoading: false,
      pagination: {
        defaultCurrent: 1,
        defaultPageSize: 10,
        total: 0,
        showPageSize: false,
        showFirstAndLastPageBtn: false,
      },
      columns: [
        {
          colKey: 'row-select',
          type: 'multiple',
          width: 64,
        },
        {
          colKey: "projectName",
          title: "项目名称",
          align: "center",
          width: 220
        },
        {
          colKey: "declareDate",
          title: "申报日期",
          align: "center",
          width: 120,
        },
        {
          colKey: "adviceFunding",
          title: "建议经费总概算",
          align: "center",
          width: 160,
        },
        {
          colKey: "subsidyFunding",
          title: import.meta.env.VITE_SITE_TAG === 'jtt' ? "厅拨经费" : `集团补助经费`,
          align: "center",
          width: 140,
        },
        {
          colKey: "projectLeaderName",
          title: "负责人",
          align: "center",
          width: 100
        },
        {
          colKey: "declareDeptName",
          title: "承担单位",
          align: "center",
          width: 180,
          // sorter: true
        },
        {
          colKey: "projectCategoryName",
          title: "计划类别",
          align: "center",
          width: 120,
        },
        {
          colKey: "subjectClassification",
          title: "学科分类",
          align: "center",
          width: 100,
        },
        {
          colKey: "projectType",
          title: "项目类型",
          align: "center",
          width: 120,
        },
        {
          colKey: "startDate",
          title: "开始时间",
          align: "center",
          width: 120,
          // sorter: true,
        },
        {
          colKey: "endDate",
          title: "结束时间",
          align: "center",
          width: 120,
          // sorter: true,
        },

        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 120
        },
      ],
      columns2: [
        {
          colKey: 'row-select',
          type: 'multiple',
          width: 64,
          checkProps: ({ row }) => ({ disabled: ['3', '4'].includes(row.xsReviewStatus) }),
        },
        {
          colKey: "projectName",
          title: "项目名称",
          align: "center",
          width: 220
        },
        {
          colKey: "declareDate",
          title: "申报日期",
          align: "center",
          width: 120,
          sorter: true,
        },
        {
          colKey: "projectLeaderName",
          title: "负责人",
          align: "center",
          width: 100
        },
        {
          colKey: "declareDeptName",
          title: "承担单位",
          align: "center",
          width: 180,
          sorter: true,
        },
        {
          colKey: "projectCategoryName",
          title: "计划类别",
          align: "center",
          width: 120
        },
        {
          colKey: "subjectClassification",
          title: "学科分类",
          align: "center",
          width: 100
        },
        {
          colKey: "projectType",
          title: "项目类型",
          align: "center",
          width: 120
        },
        {
          colKey: "startDate",
          title: "开始时间",
          align: "center",
          width: 120,
          sorter: true,
        },
        {
          colKey: "endDate",
          title: "结束时间",
          align: "center",
          width: 120,
          sorter: true,
        },
        {
          colKey: "xsReviewStatus",
          title: "形式审查状态",
          align: "center",
          width: 120,
          fixed: 'right',
          // sorter: true
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 120,
          fixed: 'right',
        },
      ],
      visible: true,
    }
  },
  async created () {
    const userId = this.$route.params && this.$route.params.userId;
    if (!userId) {
      this.$message.error("用户id丢失返回");
      this.$router.push("/kjxm/approve/approver-management");
    }
    this.userId = userId;
    await this.getDeclareTree()
    this.getList();
    this.getBindList();
  },
  methods: {
    getDeclareTree () {
      return this.$api.kjxm.project.planCategoryList().then(response => {
        this.projectCategoryOptions = this.handleTree(response.data, "id");
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    changeTab (value) {
      this.queryParams.projectCategory = value;
      this.getList();
    },
    onChange (val) {
      this.queryParams.subjectClassifications = val;
      this.getList();
    },
    joinList () {
      const newData = []
      this.data.forEach((val, index) => {
        if (this.selectedRowKeys.includes(val.id)) {
          newData.push({ ...val, rowIndex: index })
        }
      })
      this.data = this.data.filter(val => {
        if (this.selectedRowKeys.includes(val.id)) {
          return false
        }
        return true
      })
      this.data2.push(...newData)
      this.selectedRowKeys = []
    },
    removeList () {
      const newData = []
      this.data2.forEach((val, index) => {
        if (this.selectedRowKeys2.includes(val.id)) {
          newData.push(val)
        }
      })
      this.data2 = this.data2.filter(val => {
        if (this.selectedRowKeys2.includes(val.id)) {
          return false
        }
        return true
      })
      console.log(newData)
      this.data.push(...newData)
      this.selectedRowKeys2 = []
    },
    addReviewProject (projectIds) {
      this.$api.kjxm.approve.addReviewProject({
        approverId: this.row.id,
        userName: this.row.nickName,
        projectIds
      }).then((res) => {
        // 初始化
        // this.$message.success("操作成功");
        this.getList();
        this.getBindList();
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    getBindList () {
      this.dataLoading2 = true;
      this.$api.kjxm.approve.getApproverReviewByUserId(this.userId).then((res) => {
        this.data2 = res.data;
        this.dataLoading2 = false;
      }).catch((e) => {
        this.dataLoading2 = false
        this.$message.error(e.toString());
      });
    },
    getList () {
      // this.dataLoading = true;
      // this.queryParams.pageNum = this.pagination.current || 1;
      // this.$api.kjxm.approve.getApproverReviewList(this.queryParams).then((res) => {
      //   this.data = res.data;
      //   // this.pagination.total = res.total;
      //   this.dataLoading = false;
      // }).catch((e) => {
      //   this.dataLoading = false
      //   this.$message.error(e.toString());
      // });
      this.dataLoading = true;
      this.queryParams.pageSize = 1000000;
      this.$api.kjxm.approve.getApproverReviewAllList2(this.queryParams).then(response => {
        // this.pagination.total = response.total;
        this.data = response.rows;
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    onSubmit () {
      this.applyLoading = true;
      const ids = this.data2.map(val => val.id)
      this.$api.kjxm.approve.addReviewProject({
        approverId: this.userId,
        // userName: this.row.nickName,
        projectIds: ids
      }).then((res) => {
        // 初始化
        this.$message.success("分配成功");
        this.applyLoading = false;
        this.getList();
        this.getBindList();
      }).catch((e) => {
        this.$message.error(e.toString());
        this.applyLoading = false;
      });
    },
    closeDialog () {
      // this.$emit("update:visible", false);
    },

  }
}
</script>

<style scoped>
.overlay-options {
  display: inline-block;
  font-size: 20px;
}
</style>
